/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoRegion;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoCountry;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoCountryService;

import java.util.List;

/**
 * 国家管理Controller
 *
 * @author yt
 * @version 2017-10-20
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoCountry")
public class CampusinfoCountryController extends BaseController {

    @Autowired
    private CampusinfoCountryService campusinfoCountryService;

    @Autowired
    private CampusinfoRegionService campusinfoRegionService;

    @ModelAttribute
    public CampusinfoCountry get(@RequestParam(required = false) String id) {
        CampusinfoCountry entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoCountryService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoCountry();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoeducation:campusinfoCountry:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoCountry campusinfoCountry, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoCountry> page = campusinfoCountryService.findPage(new Page<CampusinfoCountry>(request, response), campusinfoCountry);
        List<CampusinfoCountry> campusinfoCountries = campusinfoCountryService.findList(campusinfoCountry);
        model.addAttribute("campusinfoCountries", campusinfoCountries);
        model.addAttribute("page", page);
        return "modules/campusinfoeducation/campusinfoCountryList";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoCountry:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoCountry campusinfoCountry, Model model) {
        CampusinfoRegion campusinfoRegion = new CampusinfoRegion();
        List<CampusinfoRegion> campusinfoRegions = campusinfoRegionService.findList(campusinfoRegion);
        model.addAttribute("campusinfoRegions", campusinfoRegions);
        model.addAttribute("campusinfoCountry", campusinfoCountry);
        return "modules/campusinfoeducation/campusinfoCountryForm";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoCountry:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoCountry campusinfoCountry, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoCountry)) {
            return form(campusinfoCountry, model);
        }
        CampusinfoCountry findcampusinfoCountry = campusinfoCountryService.getByName(campusinfoCountry);
        if(findcampusinfoCountry==null){
            campusinfoCountryService.save(campusinfoCountry);
            addMessage(redirectAttributes, "保存国家信息成功");
        } else if(!StringUtils.isBlank(campusinfoCountry.getId())){
            campusinfoCountryService.save(campusinfoCountry);
            addMessage(redirectAttributes, "修改国家信息成功");
        } else {
            addMessage(redirectAttributes, "国家已经存在");
        }
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoCountry/?repage";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoCountry:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoCountry campusinfoCountry, RedirectAttributes redirectAttributes) {
        campusinfoCountryService.delete(campusinfoCountry);
        addMessage(redirectAttributes, "删除国家信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoCountry/?repage";
    }

}